{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <div class="layui-inline">
        <div class="layui-input-inline">
            {MyTag:channel type='manage' /}
        </div>
    </div>
    <div class="layui-inline">
        <div class="layui-input-inline">
            {MyTag:game type='manage' /}
        </div>
    </div>
    <div class="layui-inline">
        <a href="javascript:;" class="layui-btn searchBtn my-btn my-btn-sm">搜索</a>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="tableBar">
    <button class="layui-btn layui-btn-green layui-btn-sm" lay-event="edit">修改状态</button>
</script>
{/block}

{block name="css"}
{/block}

{block name="js"}
<script>
    layui.use('table', function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,load = null
            ,active = {
                reload: function () {
                    var game_id = $('#game_id').val();
                    var channel_id = $('#channel_id').val();

                    table.reload('tableId', {
                        page: {curr: 1}
                        ,where: {game_id:game_id, channel_id:channel_id}
                        ,height: 'full-100'
                    })
                },
            };

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-100'
            ,url: '{:url("ChannelStatus/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function (res, curr, count) {
                layer.close(load);
                $('#layui-loading').hide();
            }
            ,cols: [[
                {field:'cps_id', title: '渠道ID', width: 100, sort:true}
                ,{field:'cps_name', title: '渠道名称', width: 200}
                ,{field:'game_id', title: '游戏ID' , width: 100, sort:true}
                ,{field:'game_name', title: '游戏名称', width: 200}
                ,{field:'name', title: '礼包名称', width: 200}
                ,{field:'give_num', title: '领取数量', width: 100}
                ,{field:'get_time', title: '领取时间', width: 170}
                ,{field:'status', title: '状态', width: 100}
                ,{title: '操作', align:'center', width: 200, toolbar:'#tableBar'}
            ]]
        });

        $('#searchBox .searchBtn').on('click', function(){
            load = layer.load();
            active['reload'].call(this);
            return false;
        });

        //监听工具条
        table.on('tool(tableBox)', function(obj){
            console.dir($(this));
            var data = obj.data;
            if(obj.event === 'edit'){
                layer.confirm('确定要修改状态吗？', {offset: '250px'}, function(index){
                    var load = layer.load();
                    $.post('{:url("ChannelStatus/edit")}',{id: data.id}, function(json){
                        layer.close(load);
                        if(json.code==0){
                            $.post('{:url("ChannelStatus/index")}',{id: data.id}, function(json){
                                obj.update({
                                    status: json.data[0].status
                                })
                            }, 'JSON').error(function(){
                                layer.close(load);
                                layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                            });

                            layer.msg(json.msg, {icon: 1, time:1000})
                        }else{
                            layer.msg(json.msg, {icon: 0, time:1000});
                        }
                    }, 'JSON').error(function(){
                        layer.close(load);
                        layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                    });
                });
            }
        });
    });
</script>
{/block}